'use strict';

/**
 * @ngdoc function
 * @name sfhtHaitaoCmsApp.controller:PageEditCtrl
 * @description
 * # PageEditCtrl
 * Controller of the sfhtHaitaoCmsApp
 */
angular.module('sfhtHaitaoCmsApp')
    .controller('ModalCreatePageCtrl', function($scope, $modalInstance, $injector, PC_HEADER_RESOURCE_TYPE, H5_HEADER_RESOURCE_TYPE, page, headers, Uploader) {
        $scope.page = page ? page : {};
        if (!$scope.page.pageConfig) {
            $scope.page.pageConfig = {};
        }
        $scope.isEdit = page ? true : false;
        $scope.title = page ? "编辑页面" : "新增页面";
        $scope.pageTypes = [{
            value: 0,
            name: 'PC'
        }, {
            value: 1,
            name: 'H5'
            // }, {
            // value: 2,
            // name: 'APP'
        }];
        $scope.headers = headers;
        // if (!$scope.isEdit) {
        //     $scope.page.pageType = $scope.pageTypes[0].value;
        //     $scope.page.header = $scope.headers[0].value;
        // }
        if (!$scope.page.pageConfig.backgroundColor) {
            $scope.page.pageConfig.backgroundColor = "#F5F5F5";
        }
        $scope.page.isShareIconCompleted = false;
        $scope.page.isBackgroundUrlCompleted = false;
        var pageService = $injector.get("PageService");
        $scope.changeFile = function(imgName) {
            Uploader.uploadFile($scope, imgName);
        };
        $scope.save = function() {
            var pageId;
            if ($scope.page && $scope.page.id > 0) {
                pageService.updatePage($scope.page)
                    .then(function(data) {
                        pageId = $scope.page.id;
                        $modalInstance.close(pageId);
                    }, function(data) {
                        alert("编辑失败:" + data);
                    })
            } else {
                pageService.createPage($scope.page)
                    .then(function(data) {
                        pageId = data.value;
                        $modalInstance.close(pageId);
                    }, function(data) {
                        alert("新增失败:" + data);
                    })
            }
        };
        $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
        };
        $scope.findHeader = function(page) {
            if (page == 0) {
                $scope.headers = headers;
                $scope.headers = _.filter($scope.headers, function(data) {
                    return data.resourceType == PC_HEADER_RESOURCE_TYPE
                })
                // if (!$scope.isEdit) {
                //     $scope.page.header = $scope.headers[0].name;
                // }
            } else {
                $scope.headers = headers;
                $scope.headers = _.filter($scope.headers, function(data) {
                    return data.resourceType == H5_HEADER_RESOURCE_TYPE
                })
                // if (!$scope.isEdit) {
                //     $scope.page.header = $scope.headers[0].name;
                // }
            }
        };
    });
